package edu.itstudy.view;

import edu.itstudy.modle.Movie;
import edu.itstudy.services.MovieServer;
import edu.itstudy.util.RandomUtil;
import edu.itstudy.util.ScannerUtil;

import java.awt.*;
import java.text.Collator;
import java.util.*;
import java.util.List;

public class MovieView {

    private MovieServer ms = new MovieServer();
    private List<Movie> sreace_movice = null;
    public List<Movie> getSreace_movice() {
        return sreace_movice;
    }
    public void setSreace_movice(List<Movie> sreace_movice) {
        this.sreace_movice = sreace_movice;
    }


    /**
     * 初始化界面
     */
    public void mianMenu(){

        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("1.影评管理\t2.查看所有影片\t3.退出系统\t");
            System.out.print("请选择序号：");
            System.out.println();
            //使用工具类输入
            char c = ScannerUtil.readMenuSelect(3);

            choose1_first(c);
        }
    }

    /**
     * 界面的下一层
     * @param choose 选择操作
     */
    public void choose1_first(char choose){

        switch(choose){
            case '1':
                System.out.println("影评管理");
                System.out.println("1.上传影片\t2.删除影片\t3.修改影片\t4.返回上一层");
                System.out.print("请选择序号：");
                char c = ScannerUtil.readMenuSelect(4);

                System.out.println();
                choose11_manageMovie(c);
                break;
            case '2':
                System.out.println("查看所有影片");
                //无影片直接返回
                if(ms.getMovies().size() == 0){
                    return ;
                }
                //打印所有影片
                ms.printMovie();
                System.out.println("1.按条件搜索影片\t2.删除影片\t3.返回上一层");
                System.out.println("请选择序号：");
                char c1 = ScannerUtil.readMenuSelect(3);

                choose12_manageMovie(c1);

                break;

            default:
                System.exit(0);//退出系统
        }
    }

    /**
     * 影评管理对应的操作界面
     * @param choose
     */
    public void choose11_manageMovie(char choose){

        switch(choose){
            case '1':
                System.out.print("上传影片，请输入对应属性");
                Scanner sc = new Scanner(System.in);
                System.out.println();

                Movie movie = addMovie();
                ms.addMovie(movie);
                System.out.println("上传成功");
                break;

            case '2':
                System.out.println("删除影片");
                ms.printMovie();
                //无影片直接返回
                if(ms.getMovies().size() == 0){
                    return ;
                }
                System.out.println("请选择要删除影片的序号");
                //int index = ScannerUtil.readInt();
                //判断序号是否存在
                Movie m = readIndex();
                boolean r = ms.removeMovie(m.getIndex());
                if(r){
                    System.out.println("影片删除成功");
                }
                break;

            case '3':
                System.out.println("修改影片");
                ms.printMovie();
                //无影片直接返回
                if(ms.getMovies().size() == 0){
                    return;
                }
                System.out.println("请选择要修改影片的序号");
                int index1 = ScannerUtil.readInt();


                Movie m1 = readIndex();
                System.out.println("请选择要修改影片的属性（不需修改直接回车）：");
                Movie movie_update = updateMovie(m1);
                boolean r1 = ms.updateMovieByIndex_obj(m1.getIndex(),movie_update);
                if(r1){
                    System.out.println("影片修改成功");
                }
                break;

            default:
                System.out.println("返回上一层");
                break;
        }
    }

    /**
     * 查看所有影片对应的操作界面
     * @param choose
     */
    public void choose12_manageMovie(char choose){

        switch(choose){
            case '1':
                System.out.print("按条件搜索影片");
                List<Movie> list = choose12_search();
                if(list == null){
                    break;
                }
               /* Scanner sc = new Scanner(System.in);
                System.out.println();*/
                //无影片直接返回
                if(ms.getMovies().size() == 0){
                    return ;
                }
                System.out.println("查询成功");
                System.out.println("1.影片排序\t,2.观看电影\t,3.推荐影片\t,4.返回影院系统\t,5.退出系统");
                char c2 = ScannerUtil.readMenuSelect(5);
                choose121(c2,list);

                break;

            case '2': {
                System.out.println("影片排序");
                ms.printMovie();
                //无影片直接返回
                if (ms.getMovies().size() == 0) {
                    return;
                }
                System.out.println("请选择要删除影片的序号");
                //int index = ScannerUtil.readInt();
                //判断序号是否存在
                Movie m = readIndex();
                boolean r = ms.removeMovie(m.getIndex());
                if (r) {
                    System.out.println("影片删除成功");
                }
                break;
            }
            default:
                System.out.println("返回上一层");
                break;
        }
    }

    /**
     * 条件过滤————影片排序下一层
     * @param  c2
     */
    public void choose121(char c2,List<Movie> list){
        switch (c2){
            case '1':
                System.out.println("影片排序,请选择排序类别：");
                System.out.println("1.按名称排序\t,2.按上映时间\t,3.按影片类型\t,4.按点击率\t,5.按推荐率\t,6.返回上一级");
                char c3 = ScannerUtil.readMenuSelect(6);
                choose1211(c3, ms.getMovies());
               ms.printMovie(list);
               /* for (Movie movie : ms.getMovies()) {
                    System.out.println(movie);
                    //sreace_movice = (List<Movie>) movie;
                }*/
                break;
            case '2':
                System.out.println("观看电影");
                System.out.println("请选择影片MID：");
                String c4 = ScannerUtil.readkeyBoard(4,false);

                ms.printMovie(ms.SearchMovie(c4,sreace_movice));
                System.out.println("正在观看");
                System.out.println();
                break;
            case '3':
                System.out.println("推荐影片");
                System.out.println("请选择影片序号：");
                String c5 = ScannerUtil.readkeyBoard(4,false);



                        /*int r =ms.gteMovieByIndex(c5).getRecommendRate();
                        ms.gteMovieByIndex(c5).setRecommendRate(r+1);*/
                /*int c =list.get();
                list.setClickRate(++c);*/
                        ms.printMovie(ms.SearchMovieFinally(c5,sreace_movice));


                //return ;
                break;
            case '4':
                break;
            default:
                System.exit(0);
                break;


        }
    }


    /**
     * 影片排序下一层
     * @param  c3
     */
    public void choose1211(char c3, List<Movie> list) {
        switch (c3) {
            case '1':
                System.out.println("按名称排序:");
                Collections.sort(ms.getMovies(), (x, y) -> Collator.getInstance(Locale.CHINA).compare(x.getName(),y.getName()));
                break;
            case '2':
                System.out.println("按上映时间");
                Collections.sort(ms.getMovies(), (x, y) ->  x.getDate().compareTo(y.getDate()));
                break;
            case '3':
                System.out.println("按影片类型");
                Collections.sort(ms.getMovies(), (x, y) ->  x.hashCode() - (y.hashCode()));
                break;
            case '4':
                System.out.println("按点击率");
                Collections.sort(ms.getMovies(), (x, y) ->  x.getClickRate() - y.getClickRate());
                break;
            case '5':
                System.out.println("按推荐率");
                Collections.sort(ms.getMovies(), (x, y) ->  x.getRecommendRate() - y.getRecommendRate());
                break;
            default:

                break;
        }

    }

    /**
     * 多条件搜索下一级
     * @param
     */
    public List<Movie> choose12_search(){
        System.out.println("请输入查询条件值");

        System.out.println("\t影片名称:");
        String name = ScannerUtil.readString(5,null);

        System.out.println("\t影片类型:");
        String type = ScannerUtil.readString(5,null);

        System.out.println("\t影片主演：");
        String performer = ScannerUtil.readString(5,null);

        System.out.println("您输入的条件为：");
        int num = 0;
        if(null != name){
            System.out.println("\t影片名称:"+name);
            num++;
        }
        if(null != type){
            System.out.println("\t影片类型:"+type);
            num++;
        }
        if(null != performer){
            System.out.println("\t影片主演:"+performer);
            num++;
        }
        if(num == 0){
            System.out.println("您没有输入查询条件:");
        }

        System.out.println("1.搜索\t2.返回上一级");
        char c = ScannerUtil.readMenuSelect(2);

       List<Movie> list = null;
       //ms.printMovie(ms.SearchMovie(name,type,performer));
       if(c == '1'){
            for (Object o : ms.SearchMovie(name,type,performer)) {
                list = ms.SearchMovie(name,type,performer);
            }
            ms.printMovie(list);
        }/*else{
           System.out.println("返回上一层");
           ms.printMovie();
           System.out.println("1.按条件搜索影片\t2.影片排序\t3.返回上一层");
           char c1 = ScannerUtil.readMenuSelect(3);
           System.out.println("请选择序号：");
           choose12_manageMovie(c1);
       }*/
        return list;
    }

    /**
     * 上传影片属性
     * @return
     */
    public Movie addMovie(){
        System.out.println("请输入电影名称:");
        String name = ScannerUtil.readString(5);
        System.out.println();

        System.out.println("请输入电影类型:");
        String type = ScannerUtil.readString(5);
        System.out.println();

        System.out.println("请输入电影主演:");
        String performer = ScannerUtil.readString(5);
        System.out.println();

        System.out.println("请输入上映时间(2021-02-02):");
        Date date = ScannerUtil.readDate("yyyy-mm-dd");
        System.out.println();

        System.out.println("请输入电影导演:");
        String guider = ScannerUtil.readString(5);
        System.out.println();

        /*System.out.println("请输入电影点击率");
        String clickRate = ScannerUtil.readString(5);

        System.out.println("请输入电影推荐率");
        String recommendRate = ScannerUtil.readString(5);*/
        //随机数作为ID
        int mid = RandomUtil.getRandomInt();

        //封装对象
        Movie movie = new Movie(mid,name,type,performer,date,guider,0,0);
        return movie;
    }

    /**
     * 修改影片属性
     * @return
     */
    public Movie updateMovie(Movie movie) {
        System.out.println("请输入电影名称("+movie.getName()+"):");
        String name = ScannerUtil.readString(5,movie.getName());
        System.out.println();

        System.out.println("请输入电影类型("+movie.getType()+"):");
        String type = ScannerUtil.readString(5,movie.getType());
        System.out.println();

        System.out.println("请输入电影主演("+movie.getPerformer()+"):");
        String performer = ScannerUtil.readString(5,movie.getPerformer());
        System.out.println();

        System.out.println("请输入上映时间(2021-02-02)("+movie.getDate()+"):");
        Date date = ScannerUtil.readDate("yyyy-mm-dd",movie.getDate());
        System.out.println();

        System.out.println("请输入电影导演("+movie.getGuider()+"):");
        String guider = ScannerUtil.readString(5,movie.getGuider());
        System.out.println();

        /*System.out.println("请输入电影点击率");
        String clickRate = ScannerUtil.readString(5);

        System.out.println("请输入电影推荐率");
        String recommendRate = ScannerUtil.readString(5);*/
        //随机数作为ID
       // int mid = RandomUtil.getRandomInt();

        //封装对象
        movie = new Movie(movie.getMid(),name,type,performer,date,guider,movie.getClickRate(),movie.getRecommendRate());
        return movie;
    }


    /**
     *判断序号是否存在
     * @param
     * @return
     */
    public Movie readIndex() {
        Movie m = null;
        while (true) {

            int index = ScannerUtil.readInt();
            //m = readIndex();
            m = ms.gteMovieByIndex(--index);


            if(m == null){
                System.out.println("输入序号不存在，请重新输入：1");
                continue;
            }
            m.setIndex(index);
            break;
        }
        return m;
    }
}
